package com.system.employee.service.impl;

import com.system.employee.bean.Employee;
import com.system.employee.bean.OptionBean;
import com.system.employee.bean.OptionsBean;
import com.system.employee.repository.EmployeeRepository;
import com.system.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 林
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;
    @Override
    public boolean saveEmployee(Employee employee) {
        Employee obj = employeeRepository.save(employee);
        if(obj != null){
            return true;
        }
        return false;
    }

    @Override
    public boolean removeEmployeeById(int id) {
        if (employeeRepository.existsById(id)) {
            employeeRepository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyEmployee(Employee employee) {
        Employee obj = employeeRepository.save(employee);
        if(obj != null){
            return true;
        }
        return false;
    }

    @Override
    public Employee getEmployeeById(int id) {
        if (employeeRepository.existsById(id)) {
            return employeeRepository.getReferenceById(id);
        }
        return null;
    }

    @Override
    public List<Employee> listAllEmployees() {
        return employeeRepository.findAll();
    }
    @Override
    public List<Employee> getListAllByDepartmentId(int departmentId){
        return employeeRepository.findAllByDepartmentId(departmentId);
    }

    @Override
    public List<Employee> getDoctorList() {
        return employeeRepository.findAllByeType(1);
    }

    @Override
    public OptionsBean<OptionBean> getDoctorOptions() {
        OptionsBean<OptionBean> options = new OptionsBean<>();
        List<OptionBean> data = new ArrayList<>();
        List<Employee> employeeList = employeeRepository.findAllByeType(1);
        for (Employee e : employeeList){
            OptionBean option = new OptionBean();
            option.setLabel(e.getEName());
            option.setValue(Long.valueOf(e.getEmployeeId()));
            data.add(option);
        }
        options.setCode("00000");
        options.setMsg("ok");
        options.setData(data);
        return options;
    }
}
